package com.github.zzay.care.controller;

import com.github.zzay.care.dto.VolunteerDto;
import com.github.zzay.care.entity.Volunteer;
import com.github.zzay.care.service.VolunteerService;
import com.github.zzay.common.core.model.ResponseBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className VolunteerController
 * @description Controller class of "Volunteer" related API
 * @create 2022/07/07 13:27
 */
@Slf4j
@RestController
@RequestMapping("/volunteer")
@CrossOrigin
@Tag(name = "VolunteerController", description = "义工信息操作相关接口")
public class VolunteerController {

    /**
     * Volunteer Service.
     */
    @Autowired
    private VolunteerService volunteerService;

    /**
     * Add a volunteer into the system.
     *
     * @param volunteerDto Volunteer DTO
     * @return Operation Result
     */
    @Operation(summary = "录入义工信息", description = "录入义工信息：系统管理员通过录入表单输入相关信息，执行录入操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "录入信息成功"),
            @ApiResponse(responseCode = "400", description = "录入信息失败")
    })
    @PostMapping("/add")
    public ResponseBean<Map<String, Object>> addVolunteer(
            @Parameter(name = "volunteerDto") @RequestBody VolunteerDto volunteerDto) {
        Map<String, Object> data = new HashMap<>();
        // get related information
        String name = volunteerDto.getName();
        String idCard = volunteerDto.getIdCard();
        String sex = volunteerDto.getSex();
        String phone = volunteerDto.getPhone();
        Date birthday = volunteerDto.getBirthday();
        Date checkInDate = volunteerDto.getCheckInDate();
        Volunteer volunteer = new Volunteer(name, idCard, sex, phone, birthday, checkInDate);
        // save the new volunteer
        Boolean success = volunteerService.save(volunteer);
        data.put("success", success);
        // add a new volunteer successfully
        if (success) {
            log.info("======== AddVolunteer - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully add a new volunteer", data);
        }
        // failed to add a new volunteer
        log.info("======== AddVolunteer - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to add a new volunteer", data);
    }

    /**
     * Delete a volunteer's information.
     *
     * @param volunteerDto Volunteer DTO
     * @return Operation Result
     */
    @Operation(summary = "删除义工信息", description = "删除义工信息：系统管理员通过删除表单输入相关信息，执行删除操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除信息成功"),
            @ApiResponse(responseCode = "400", description = "删除信息失败")
    })
    @PostMapping("/delete")
    public ResponseBean<Map<String, Object>> deleteVolunteer(
            @Parameter(name = "volunteerDto") @RequestBody VolunteerDto volunteerDto) {
        Map<String, Object> data = new HashMap<>();
        Boolean success = volunteerService.deleteInfo(volunteerDto.getIdCard());
        data.put("success", success);
        // delete a volunteer's information successfully
        if (success) {
            log.info("======== DeleteVolunteer - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully delete the information", data);
        }
        // failed to delete a volunteer's information
        log.info("======== DeleteVolunteer - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to delete the information", data);
    }

    /**
     * Update a volunteer's information.
     *
     * @param volunteerDto Volunteer DTO
     * @return Operation Result
     */
    @Operation(summary = "更新义工信息", description = "更新义工信息：系统管理员通过更新表单输入相关信息，执行更新操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新信息成功"),
            @ApiResponse(responseCode = "400", description = "更新信息失败")
    })
    @PostMapping("/update")
    public ResponseBean<Map<String, Object>> updateVolunteer(
            @Parameter(name = "volunteerDto") @RequestBody VolunteerDto volunteerDto) {
        Map<String, Object> data = new HashMap<>();
        // update the entity
        Boolean success = volunteerService.updateInfo(volunteerDto);
        data.put("success", success);
        // update a volunteer's information successfully
        if (success) {
            log.info("======== UpdateVolunteer - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully update the information", data);
        }
        // failed to update a volunteer's information
        log.info("======== UpdateVolunteer - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to update the information", data);
    }

    /**
     * Query all information.
     *
     * @return Operation Result
     */
    @Operation(summary = "查询所有老人信息", description = "查询所有老人信息：系统查询所有老人信息并返回",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getAll")
    public ResponseBean<Map<String, Object>> getAllVolunteer() {
        Map<String, Object> data = new HashMap<>();
        List<VolunteerDto> volunteerList = volunteerService.getAll();
        if (volunteerList == null) {
            // failed to get information
            data.put("success", false);
            log.info("======== GetAllVolunteer - Fail ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get information successfully
            data.put("success", true);
            data.put("info", volunteerList);
            log.info("======== GetAllVolunteer - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query a volunteer's information according to the Id card number.
     *
     * @param volunteerDto Volunteer DTO
     * @return Operation Result
     */
    @Operation(summary = "查询义工信息", description = "查询义工信息：系统管理员根据义工身份证号，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getById")
    public ResponseBean<Map<String, Object>> getVolunteerById(
            @Parameter(name = "volunteerDto") @RequestBody VolunteerDto volunteerDto) {
        Map<String, Object> data = new HashMap<>();
        VolunteerDto volunteer = volunteerService.getInfoById(volunteerDto.getIdCard());
        if (volunteer == null) {
            // failed to get a volunteer's information
            data.put("success", false);
            log.info("======== GetVolunteerById - Success ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get a volunteer's information successfully
            data.put("success", true);
            log.info("======== GetVolunteerById - Fail ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query a volunteer's information according to the name.
     *
     * @param volunteerDto Volunteer DTO
     * @return Operation Result
     */
    @Operation(summary = "查询义工信息", description = "查询义工信息：系统管理员根据义工姓名，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getByName")
    public ResponseBean<Map<String, Object>> getVolunteerByName(
            @Parameter(name = "volunteerDto") @RequestBody VolunteerDto volunteerDto) {
        Map<String, Object> data = new HashMap<>();
        VolunteerDto volunteer = volunteerService.getInfoByName(volunteerDto.getName());
        if (volunteer == null) {
            // failed to get a volunteer's information
            data.put("success", false);
            log.info("======== GetVolunteerByName - Success ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get a volunteer's information successfully
            data.put("success", true);
            log.info("======== GetVolunteerByName - Fail ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

}
